package com.xdf.ecommerce.stream.defaultChannel;

import com.alibaba.fastjson.JSON;
import com.xdf.ecommerce.vo.SCSMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;

/**
 * 使用默认的通信信道实现消息的发送
 * @author chanchaw
 * @create 2025-11-04 20:33
 */
@Slf4j
@EnableBinding(Source.class)
@RequiredArgsConstructor
public class DefaultSendService {
    private final Source source;

    /**
     * 使用默认的输出信道发送消息
     * @param message 传入实体类对象，要转换为 JSON 字符串
     */
    public void sendMessage(SCSMessage message){
        String msg = JSON.toJSONString(message);
        log.info("即将发送的消息体对象转换json string后结果是：[{}]", msg);
        source.output().send(MessageBuilder.withPayload(msg).build());
    }
}
